package StudyCode.summary.BufferAndChannel;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreatingCharViewByteBuffer {
	public static void main(String[] argv) throws Exception {
		ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN);
		CharBuffer charBuffer = byteBuffer.asCharBuffer();

		byteBuffer.put(0, (byte) 0);
		byteBuffer.put(1, (byte) 'H');
		byteBuffer.put(2, (byte) 0);
		byteBuffer.put(3, (byte) 'i');
		byteBuffer.put(4, (byte) 0);
		byteBuffer.put(5, (byte) '!');
		byteBuffer.put(6, (byte) 0);

		println(byteBuffer);
		println(charBuffer);
	}

	private static void println(Buffer buffer) {
		System.out.println("位置=" + buffer.position() + ", limit=" + buffer.limit()
				+ ", 容量=" + buffer.capacity() + ": '" + buffer.toString() + "'");
	}
}
